********************************************************************************
** 	TITLE: e21_sag_gen_eventstudy.do

**	PROJECT: IGNITE
** 
**  PURPOSE: Appendix Figure XXI: Effects of IGNITE on Recidivism: Event Study Estimates
********************************************************************************
**  PURPOSE: Event Study for Genesee and Saginaw Diff-in-Diff
********************************************************************************

set sortseed 13
		//Obtaining the estimate sample from Genesee County 
	use "$output_data/estimate.dta", replace
	est clear

		//Obtaining the estimate sample from Saginaw County 	
	append using "$output_data/sag_post_roa_all.dta"

	keep inmate booking_date release_date genesee e_sample stay_rec ignite_0_rec
	
	tempfile basesaggen
	
	save `basesaggen', replace

		//Obtaining recidivism measures for individuals in their original county of booking
use "$output_data/sag_gen_appended.dta", clear

merge 1:1 inmate booking_date release_date genesee using `basesaggen', keep(3) keepusing(e_sample stay_rec ignite_0_rec) assert(1 3) nogen

				//stay ignite_0 for Recidivism
				gen stay = stay_rec
				gen ignite_0 = ignite_0_rec				

		* event study
forval j=4/4{
preserve
mat R = J(200,3,.)

local binsize = `j'
cap drop bin_`binsize'm
gen bin_`binsize'm = .
levelsof ym_booked, local(ym_li)

local j=1
local i=0
foreach ym in `ym_li'{
	local i=`i'+1
	if `i'<=`binsize'{
		qui replace bin_`binsize'm = `j' if ym_booked==`ym'
	}
	else{
		local i=1
		local j=`j'+1
		qui replace bin_`binsize'm = `j' if ym_booked==`ym'
	}
}

su bin_`binsize'm if ym==720
local base = r(mean)-1
local y return_3m

reghdfe `y'   ///
		ib`base'.bin_`binsize'm##genesee  ///
		if `y'>=0& (genesee|(!genesee&e_sample)), ///
		cluster(inmate) a(i.quarter_booked##genesee)
		
levelsof bin_`binsize'm if e(sample), local(ym_li)

local j=1
foreach ym in `ym_li'{
	mat R[`j',1] = _b[`ym'.bin_`binsize'm#1.genesee]
	mat R[`j',2] = _se[`ym'.bin_`binsize'm#1.genesee]
	mat R[`j',3] = `ym'
	local j=`j'+1
}
restore

preserve

svmat R
keep if !mi(R1)


gen ub = 1.96*R2+R1
gen lb = -1.96*R2+R1
replace R3 = R3-`base'-1

* shades

set obs `=_N+2'
replace R3 = -.5 if _n==_N-1
replace R3 = 1.5 if _n==_N

gen shade_max = .1 if _n>=_N-1
gen shade_min = -.15 if _n>=_N-1


tw (rarea shade_max shade_min R3, lc(%0) fc(gs10%30)) ///
	(scatter R1 R3, mc(black)) (rcap ub lb R3, lc(black)) , ///
	yline(0, lc(black)) ///
	xtitle("`binsize'-Month Bins") ///
	ytitle("3-Month Recidivism") ///
	legend(off) ///
	aspect(.6) ///
	ylab(-.15(.05).1) xlab(-12(4)8) ///
	xscale(range(-13 8)) ysc(titlegap(-5))
	
gr export "$figures/sag_gen_es_shifted_`binsize'm.pdf", replace

restore
}
